import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class MyAppBar extends StatefulWidget implements PreferredSizeWidget {
  const MyAppBar(
      {Key? key,
      this.backgroundColor: Colors.white,
      required this.title,
      this.actionName: "", // 右边的文字
      this.onPressed,
      this.isBack: true})
      : super(key: key);

  final Color backgroundColor;
  final String title;

  final String actionName;
  final VoidCallback? onPressed; // 表示这个值可能为null
  final bool isBack;

  @override
  _MyAppBarState createState() => _MyAppBarState();

  @override
  Size get preferredSize => Size.fromHeight(48.0);
}

class _MyAppBarState extends State<MyAppBar> {
  SystemUiOverlayStyle _overlayStyle = SystemUiOverlayStyle.light;

  @override
  void initState() {
    super.initState();
    setState(() {
      _overlayStyle =
          ThemeData.estimateBrightnessForColor(widget.backgroundColor) ==
                  Brightness.dark
              ? SystemUiOverlayStyle.light
              : SystemUiOverlayStyle.dark;
    });
  }

  Color getColor() {
    return _overlayStyle == SystemUiOverlayStyle.light
        ? Colors.white
        : Colors.black;
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        color: widget.backgroundColor,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    alignment: Alignment.center,
                    width: double.infinity,
                    child: Text(widget.title,
                        style: TextStyle(
                          fontSize: 18,
                          color: getColor(),
                        )),
                    padding: const EdgeInsets.symmetric(horizontal: 48.0),
                  )
                ],
              ),
              widget.isBack
                  ? IconButton(
                      onPressed: () {
                        Navigator.maybePop(context);
                      },
                      padding: const EdgeInsets.all(12.0),
                      icon: Icon(Icons.arrow_back),
                    )
                  : SizedBox(),
              Positioned(
                right: 0.0,
                child: Theme(
                  data: ThemeData(
                      buttonTheme: ButtonThemeData(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    minWidth: 60.0,
                  )),
                  child: widget.actionName.isEmpty
                      ? Container()
                      : TextButton(
                          child: Text(widget.actionName),
                          onPressed: widget.onPressed,
                        ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
